package com.hyphenate.easeui.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hyphenate.easeui.R;

import java.util.List;

/**
 * primary menu
 *
 */
public class EaseChatPrimaryMenu extends EaseChatPrimaryMenuBase implements OnClickListener {
    private EditText editText;
    private RelativeLayout edittext_layout;
    private View buttonSend;
    private View buttonPressToSpeak;
    private Context context;
    private EaseVoiceRecorderView voiceRecorderView;
    private LinearLayout lin1,lin2,lin3,lin4,lin5,lin6,lin7;
    private ImageView lin1_img;
    private LinearLayout ease_chat_primary_bottom;
    public EaseChatPrimaryMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public EaseChatPrimaryMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EaseChatPrimaryMenu(Context context) {
        super(context);
        init(context, null);
    }
    public void setPrimaryMenuBottomGone(List<Integer> gonelist){
        this.gonelist=gonelist;
        if(super.gonelist!=null){
            for(int i=0;i<gonelist.size();i++){
                if(gonelist.get(i)==1){
                    lin1.setVisibility(View.GONE);
                }else if(gonelist.get(i)==2){
                    lin2.setVisibility(View.GONE);
                }else if(gonelist.get(i)==3){
                    lin3.setVisibility(View.GONE);
                }else if(gonelist.get(i)==4){
                    lin4.setVisibility(View.GONE);
                }else if(gonelist.get(i)==5){
                    lin5.setVisibility(View.GONE);
                }else if(gonelist.get(i)==6){
                    lin6.setVisibility(View.GONE);
                }else if(gonelist.get(i)==7){
                    lin7.setVisibility(View.GONE);
                }
            }
        }

    }
    //设置底部菜单是否隐藏 1 显示  2隐藏
    public void setEaseChatPrimaryBottomVISIBLE(int type){
          if(type==View.VISIBLE){
              ease_chat_primary_bottom.setVisibility(View.VISIBLE);
          }else{
              ease_chat_primary_bottom.setVisibility(View.GONE);
          }
    }
    private void init(final Context context, AttributeSet attrs) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.ease_widget_chat_primary_menu, this);
        editText = (EditText) findViewById(R.id.et_sendmessage);
        edittext_layout = (RelativeLayout) findViewById(R.id.edittext_layout);
        buttonSend = findViewById(R.id.btn_send);
        buttonPressToSpeak = findViewById(R.id.btn_press_to_speak);
        lin1 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin1);
        lin1_img=(ImageView)findViewById(R.id.ease_chat_primary_img1);
        lin2 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin2);
        lin3 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin3);
        lin4 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin4);
        lin5 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin5);
        lin6 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin6);
        lin7 = (LinearLayout) findViewById(R.id.ease_chat_primary_lin7);
        ease_chat_primary_bottom= (LinearLayout) findViewById(R.id.ease_chat_primary_rg);
//        edittext_layout.setBackgroundResource(R.drawable.ease_input_bar_bg_normal);

        buttonSend.setOnClickListener(this);
        lin1.setOnClickListener(this);
        lin2.setOnClickListener(this);
        lin3.setOnClickListener(this);
        lin4.setOnClickListener(this);
        lin5.setOnClickListener(this);
        lin6.setOnClickListener(this);
        lin7.setOnClickListener(this);
        editText.setOnClickListener(this);
        editText.requestFocus();
        
//        editText.setOnFocusChangeListener(new OnFocusChangeListener() {
//
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    edittext_layout.setBackgroundResource(R.drawable.ease_input_bar_bg_active);
//                } else {
//                    edittext_layout.setBackgroundResource(R.drawable.ease_input_bar_bg_normal);
//                }
//
//            }
//        });
        // listen the text change
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    buttonSend.setVisibility(View.VISIBLE);
                } else {
                    buttonSend.setVisibility(View.GONE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        
        
        buttonPressToSpeak.setOnTouchListener(new OnTouchListener() {
            
            @Override 
            public boolean onTouch(View v, MotionEvent event) {
                if(listener != null){
                    return listener.onPressToSpeakBtnTouch(v, event);
                }
                return false;
            }
        });
    }
    
    /**
     * set recorder view when speak icon is touched
     * @param voiceRecorderView
     */
    public void setPressToSpeakRecorderView(EaseVoiceRecorderView voiceRecorderView){
        this.voiceRecorderView = voiceRecorderView;
    }

    /**
     * append emoji icon to editText
     * @param emojiContent
     */
    public void onEmojiconInputEvent(CharSequence emojiContent){
        editText.append(emojiContent);
    }
    
    /**
     * delete emojicon
     */
    public void onEmojiconDeleteEvent(){
        if (!TextUtils.isEmpty(editText.getText())) {
            KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
            editText.dispatchKeyEvent(event);
        }
    }
    private boolean isText=true;
    /**
     * on clicke event
     * @param view
     */
    @Override
    public void onClick(View view){
        int id = view.getId();
        if (id == R.id.btn_send) {
            if(listener != null){
                String s = editText.getText().toString();
                editText.setText("");
                listener.onSendBtnClicked(s);
            }
        }  else if (id == R.id.et_sendmessage) {

            if(listener != null)
                listener.onEditTextClicked();
        } else if (id == R.id.ease_chat_primary_lin1) {
            if(isText) {
                isText=false;
                setModeVoice();

                if (listener != null)
                    listener.onToggleVoiceBtnClicked();
            }else{
                isText=true;
                setModeKeyboard();

                if(listener != null)
                    listener.onToggleVoiceBtnClicked();
            }
        }else if (id == R.id.ease_chat_primary_lin2) {
            listener.onChatPrimaryBottom_Lin2Clicked();
        }else if (id == R.id.ease_chat_primary_lin3) {
            listener.onChatPrimaryBottom_Lin3Clicked();
        }else if (id == R.id.ease_chat_primary_lin4) {
            listener.onChatPrimaryBottom_Lin4Clicked();
        }else if (id == R.id.ease_chat_primary_lin5) {
            listener.onChatPrimaryBottom_Lin5Clicked();
        }else if (id == R.id.ease_chat_primary_lin6) {

            if(listener != null){
                listener.onToggleEmojiconClicked();
            }
            if(!isText){
                isText=true;
                setModeKeyboard();

                if(listener != null)
                    listener.onToggleVoiceBtnClicked();
            }
        } else if (id == R.id.ease_chat_primary_lin7) {
//            buttonSetModeVoice.setVisibility(View.VISIBLE);
//            buttonSetModeKeyboard.setVisibility(View.GONE);
            edittext_layout.setVisibility(View.VISIBLE);
            buttonPressToSpeak.setVisibility(View.GONE);

            if(listener != null)
                listener.onToggleExtendClicked();
        }
    }
    
    
    /**
     * show voice icon when speak bar is touched
     * 
     */
    protected void setModeVoice() {
        hideKeyboard();
        edittext_layout.setVisibility(View.GONE);
        lin1_img.setImageResource(R.drawable.ease_primary_img1_k);
        buttonSend.setVisibility(View.GONE);
        buttonPressToSpeak.setVisibility(View.VISIBLE);

    }

    /**
     * show keyboard
     */
    protected void setModeKeyboard() {
        edittext_layout.setVisibility(View.VISIBLE);
        lin1_img.setImageResource(R.drawable.ease_primary_img1);
        editText.requestFocus();
        // buttonSend.setVisibility(View.VISIBLE);
        buttonPressToSpeak.setVisibility(View.GONE);
        if (TextUtils.isEmpty(editText.getText())) {
            buttonSend.setVisibility(View.GONE);
        } else {
            buttonSend.setVisibility(View.VISIBLE);
        }

    }
    
    

    

    @Override
    public void onExtendMenuContainerHide() {

    }

    @Override
    public void onTextInsert(CharSequence text) {
       int start = editText.getSelectionStart();
       Editable editable = editText.getEditableText();
       editable.insert(start, text);
       setModeKeyboard();
    }

    @Override
    public EditText getEditText() {
        return editText;
    }

}
